﻿using System;
using System.Collections.Generic;
using System.Text;
using DevExpress.ExpressApp.DC;
using DevExpress.Persistent.Base;
using DevExpress.Xpo;
using DevExpress.Persistent.Validation;
using DevExpress.ExpressApp;
using DevExpress.Data.Filtering;
using DevExpress.ExpressApp.SystemModule;
using DevExpress.ExpressApp.Actions;

namespace DomainComponents.XCRM {
    public enum ProductStatus {
        Inactive = 0,
        Active = 1
    }

    public class ProductValidationRules {
        public const string NameIsRequired = "ProductNameIsRequired";
        public const string PriceIsGreaterThanOrEqualZero = "PriceIsGreaterThanOrEqualZero";
    }

    [DomainComponent, XafDefaultProperty("Name")]
    [VisibleInReports]
    [ImageName("BO_Product")]
    [ListViewFilter("Product_ActiveProducts", "Status='Active'", "Active Products", true, Index = 0)]
    [ListViewFilter("Product_AllProducts", "", "All Products", Index = 1)]
    [ListViewFilter("Product_InactiveProducts", "Status='Inactive'", "Inactive Products", Index = 2)]
    public interface IProduct {
        [RuleRequiredField(ProductValidationRules.NameIsRequired, DefaultContexts.Save)]
        string Name { get; set; }

        [RuleValueComparison(ProductValidationRules.PriceIsGreaterThanOrEqualZero, DefaultContexts.Save, ValueComparisonType.GreaterThanOrEqual, 0)]
        decimal Price { get; set; }
        
        [System.ComponentModel.DefaultValue(ProductStatus.Active)]
        ProductStatus Status { get; set; }
        
        ICategory Category { get; set; }
        
        [Action(PredefinedCategory.RecordEdit, Caption="Activate Product...",AutoCommit=true, 
            ConfirmationMessage="This operation will set the selected Product as Active.", TargetObjectsCriteria="Status != 'Active'",
            SelectionDependencyType=MethodActionSelectionDependencyType.RequireSingleObject)]
        void Activate();
        
        [Action(PredefinedCategory.RecordEdit, Caption = "Deactivate Product...", AutoCommit = true,
            ConfirmationMessage = ProductLogic.DeactivateConfirmationMessage, TargetObjectsCriteria = "Status = 'Active'",
            SelectionDependencyType = MethodActionSelectionDependencyType.RequireSingleObject)]
        void Deactivate();
    }

    [DomainLogic(typeof(IProduct))]
    public class ProductLogic {
        public const string DeactivateConfirmationMessage = 
@"This action will set the Product as inactive. There may be records in the sytem that continue to reference these inactive records.

If you want to continue, click Yes.
If you don't want to deactivate this Product, click No.";

        public static void Deactivate(IProduct product) {
            product.Status = ProductStatus.Inactive;
        }
        public static void Activate(IProduct product) {
            product.Status = ProductStatus.Active;
        }
    }
}

